package com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.ui.medicinereminder;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.R;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.ActivityMedicineAlarm;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.doctor.ActivityMedicineCompleteDoctor;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.ActivityMedicineFeedback;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.ActivityMedicineService;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.doctor.ActivityMedicineInforDoctor;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.parent.ActivityMedicineCompleteParent;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.parent.ActivityMedicineInforParent;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.databinding.FragmentMedicineReminderBinding;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.UserViewModel;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.GlobalViewModelStore;
import com.google.android.material.appbar.MaterialToolbar;

public class MedicineReminderFragment extends Fragment {

    private FragmentMedicineReminderBinding binding;
    private MedicineReminderViewModel mViewModel;
    private UserViewModel userViewModel;


    public static MedicineReminderFragment newInstance() {
        return new MedicineReminderFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentMedicineReminderBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        setUserViewModel();

        setButtonActionListener(view);

        setToolBar(view);

        return view;
    }

    private void setUserViewModel(){
        // 获取全局共享的 UserViewModel
        userViewModel = GlobalViewModelStore.getInstance(requireActivity().getApplication())
                .get(UserViewModel.class);

    }

    private void setButtonActionListener(View view){
        View btn1 = view.findViewById(R.id.btn_1);
        View btn2 = view.findViewById(R.id.btn_2);
        View btn3 = view.findViewById(R.id.btn_3);
        View btn4 = view.findViewById(R.id.btn_4);
        View btn5 = view.findViewById(R.id.btn_5);

        btn1.setOnClickListener(v->{navigate(requireContext(), ActivityMedicineAlarm.class);});
        btn2.setOnClickListener(v->{
            var targetActivity = userViewModel.isParent()?ActivityMedicineInforParent.class: ActivityMedicineInforDoctor.class;
            navigate(requireContext(), targetActivity);
        });
        btn3.setOnClickListener(v->{navigate(requireContext(), ActivityMedicineService.class);});
        btn4.setOnClickListener(v->{navigate(requireContext(), ActivityMedicineFeedback.class);});
        btn5.setOnClickListener(v->{
            var targetActivity = userViewModel.isParent()?ActivityMedicineCompleteParent.class: ActivityMedicineCompleteDoctor.class;
            navigate(requireContext(), targetActivity);
        });
    }

    private void setToolBar(View view) {
        // 找到 Toolbar
        MaterialToolbar toolbar = view.findViewById(R.id.toolbar);
        // 设置返回按钮点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                var targetActivity = (userViewModel.isParent())? R.id.nav_host_fragment_activity_home_parent : R.id.nav_host_fragment_activity_home_doctor;

                // 根据当前用户身份导航回 对应的 HomeFragment
                NavController navController = Navigation.findNavController(requireActivity(), targetActivity);
                navController.popBackStack();
            }
        });
    }

    public void navigate(Context fromContext, Class<? extends Context> toContext) {
        Intent intent = new Intent(fromContext, toContext);
        fromContext.startActivity(intent);
    }

}